/* Copyright (C) 2007-2008  klevgert  http://code.google.com/p/db4o-netbeans/

This file is part of the db4o-netbeans module suite.

db4o-netbeans is free software; you can redistribute it and/or modify it under
the terms of version 2 of the GNU General Public License as published
by the Free Software Foundation and as clarified by db4objects' GPL 
interpretation policy, available at
http://www.db4o.com/about/company/legalpolicies/gplinterpretation/
Alternatively you can write to db4objects, Inc., 1900 S Norfolk Street,
Suite 350, San Mateo, CA 94403, USA.

db4o-netbeans is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
for more details.

You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. */

package com.db4o.nb.editor.nodes;

import com.db4o.ObjectContainer;
import com.db4o.nb.api.Db4oDatabase;
import com.db4o.nb.api.Db4oProvider;
import com.db4o.nb.api.exception.Db4oProviderException;
import com.db4o.nb.util.DialogUtil;
import java.util.ArrayList;
import java.util.List;
import org.openide.loaders.DataNode;
import org.openide.loaders.DataObject;
import org.openide.nodes.AbstractNode;
import org.openide.nodes.Children;
import org.openide.nodes.Node;
import org.openide.util.NbBundle;

/**
 * This is the data node used by the editor.
 * The data node is connected to a data object which in turn
 * hosts access to the db4o data.
 *
 * @author klevgert
 */
public class Db4oDataNode extends DataNode {

  static final String ICON_PATH = "com/db4o/nb/editor/resources/database.gif";

  private Db4oProvider provider;
  
  /**
   * Creates a new instance of Db4oDataNode
   */
  public Db4oDataNode(DataObject dataObject) {
    super(dataObject, new Db4oDataNodeChildren(dataObject));
    
    this.provider = (Db4oProvider)dataObject.getCookie(Db4oProvider.class);
    this.provider.getDatabase().connect();
      
    ObjectContainer oc = this.provider.getDatabase().getObjectContainer();
    String displayName = ( oc != null )? oc.toString() : "???";
    this.setDisplayName(displayName);
    this.setIconBaseWithExtension(ICON_PATH);
  }
  
  // <editor-fold defaultstate="collapsed" desc=" Children ">                          

  public static class Db4oDataNodeChildren extends Children.Keys {

    private DataObject dataObject;
    private Db4oProvider provider;

    /** Creates a new instance of DatabaseChildren */
    public Db4oDataNodeChildren(DataObject dataObject) {
      
      this.dataObject = dataObject;
      this.provider = (Db4oProvider)dataObject.getCookie(Db4oProvider.class);
 
    }

    protected Node[] createNodes(Object obj) {
      
      AbstractNode node = null;
      
      if( obj instanceof Db4oDatabase ) {
        
        node = new DatabaseNode( (Db4oDatabase)obj, this.dataObject );
      
      } else if( obj instanceof List ) {
        
        node = new QueryListNode( (List)obj, this.dataObject);
        
      }
      return new Node[] { node };
    }  

    protected void addNotify() {

      ArrayList keys = new ArrayList(); 
      keys.add( this.provider.getDatabase() );
      keys.add( this.provider.getQueries());
      setKeys (keys.toArray());
    }  

  }
  // </editor-fold>                          

}
